/*
 * @Descripttion: 
 * @version: V1.0
 * @Author: ZYP  2305592180@qq.com
 * @Date: 2025-07-29 14:21:49
 * @LastEditors: ZYP
 */
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#ifndef __ZYP_I2C_H
#define __ZYP_I2C_H


typedef struct {
    uint8_t device_addr;
    uint16_t voltage;
    uint16_t current;
    uint16_t power;
    TickType_t timestamp;
} ina219_data_t;
extern QueueHandle_t ina219_queue;
// extern uint8_t device_addresses[] = {0x40, 0x41, 0x44, 0x45};
// #define NUM_DEVICES (sizeof(device_addresses) / sizeof(device_addresses[0]))


#define ZP_I2C_MASTER_NUM_0             I2C_NUM_0  
#define ZP_I2C_MASTER_NUM_1             I2C_NUM_1


#define ZP_I2C_MASTER_FREQ_HZ         100000          // I2C时钟频率100kHz       100？




#define INA219_CONFIGURATION        0x00
#define INA219_SHUNT_VOLTAGE        0x01
#define INA219_BUS_VOLTAGE          0x02
#define INA219_POWER                0x03
#define INA219_CURRENT              0x04
#define INA219_CALIBRATION          0x05

// 配置寄存器位定义 [1,3](@ref)
#define INA219_CONFIG_RESET         (0x8000)
#define INA219_CONFIG_BVOLTAGE_16V  (0x0000)  // 0-16V量程
#define INA219_CONFIG_GAIN_80MV     (0x0800)  // ±80mV分流电压量程
#define INA219_CONFIG_BADC_12BIT    (0x0180)  // 12位总线ADC精度
#define INA219_CONFIG_SADC_12BIT    (0x0018)  // 12位分流ADC精度
#define INA219_CONFIG_MODE_CONT     (0x0007)  // 连续采样模式

// 全局校准参数
static float g_currentLSB;          // 电流分辨率 (A/bit)
static uint16_t g_calibrationValue; // 校准寄存器值


#define READ_I2C_SLAVE_ADDR_1                  0x40                      /// 0b1000000      
#define READ_I2C_SLAVE_ADDR_2                  0b1000001     
#define READ_I2C_SLAVE_ADDR_3                  0b1000100      
#define READ_I2C_SLAVE_ADDR_4                  0b1000100

#define MAX_RANGE                  16.0
#define MAX_CURRENT                 3.2
#define SHUNT_RESISTOR             0.1
float ina219_read_bus_voltage_v(i2c_port_t i2c_num,uint8_t dev_addr);
float ina219_read_current_ma(i2c_port_t i2c_num,uint8_t dev_addr);
float ina219_read_power_mw(i2c_port_t i2c_num,uint8_t dev_addr);
float ina219_read_shunt_voltage_mv(i2c_port_t i2c_num,uint8_t dev_addr);



esp_err_t ina219_configure(i2c_port_t i2c_num,uint8_t dev_addr, float maxVoltage, float maxCurrent, float shuntResistor);
esp_err_t zyp_i2c_master_init(i2c_port_t i2c_num,int sda_io_num,int scl_io_num);
esp_err_t zyp_i2c_read_register_one(i2c_port_t i2c_num,uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, size_t len);      ///用于读取任意长度的数据，且可以动态处理多个字节的读取  使用了 i2c_master_read 来处理多个字节的读取，而 i2c_master_read_byte 用于读取最后一个字节并发送 I2C_MASTER_NACK
esp_err_t zyp_i2c_read_registe_two(uint8_t reg_addr, uint8_t *data,size_t slave_addr);


#define ZP_I2C_MASTER_SCL_IO           GPIO_NUM_9
#define ZP_I2C_MASTER_SDA_IO           GPIO_NUM_8
#define ZP_I2C_MASTER_FREQ_HZ          100000
#define ZP_I2C_MASTER_TX_BUF_DISABLE   0
#define ZP_I2C_MASTER_RX_BUF_DISABLE   0

#define INA219_ADDR                 0x40  // 7位地址[7](@ref)
#define INA219_CONFIG               0x00
#define INA219_SHUNT_VOLTAGE        0x01
#define INA219_BUS_VOLTAGE          0x02
#define INA219_POWER                0x03
#define INA219_CURRENT              0x04
#define INA219_CALIBRATION          0x05

#define CONFIG_RST          (0 << 15)
#define CONFIG_BRNG         (0 << 13)   // 16V量程           
// #define CONFIG_PG           (0x03 << 11) // ±320mV        bit11 bit12  1 1   /8 ±320 mV      见手册 8.6.2.1 Configuration Register (address = 00h) [reset = 399Fh]

#define CONFIG_PG           (0x00 << 11) // ±40 mV

#define CONFIG_BADC         (0x0C << 7)  // 12位分辨率/16次平均
#define CONFIG_SADC         (0x0C << 3)  // 12位分辨率/16次平均

#define CONFIG_MODE_Vatage   (0x06)        /// just only  atage
#define CONFIG_MODE         (0x07)      // 连续检测模式
 
#define CONFIG_VALUE        ((CONFIG_RST | CONFIG_BRNG | CONFIG_PG | CONFIG_BADC | CONFIG_SADC | CONFIG_MODE))

#define CONFIG_VALUE_ONLY_Vatage  ((CONFIG_RST | CONFIG_BRNG | CONFIG_PG | CONFIG_BADC | CONFIG_SADC | CONFIG_MODE_Vatage))

// 校准值计算 (保持原始计算)[7](@ref)
#define CALIBRATION_VALUE   0x1000  // 4096
#define CURRENT_LSB         0.0001f // 0.1mA
#define POWER_LSB           0.002f  // 2mW

esp_err_t ina219_init_third(i2c_port_t i2c_num,uint8_t device_addr);
esp_err_t ina219_get_bus_voltage_third(i2c_port_t i2c_num,uint8_t dev_addr,uint16_t *voltage);
esp_err_t ina219_get_current_third(i2c_port_t i2c_num, uint8_t dev_addr, uint16_t *current);
esp_err_t ina219_get_power_third(i2c_port_t i2c_num, uint8_t dev_addr, uint16_t *power);

esp_err_t ina219_write_register_third(i2c_port_t i2c_num,uint8_t device_addr,uint8_t reg, uint16_t value);
esp_err_t ina219_read_register_third(i2c_port_t i2c_num, uint8_t dev_addr, uint8_t reg, uint16_t *value);  // 读取16位寄存器
esp_err_t ina219_read_register(i2c_port_t i2c_num, uint8_t dev_addr, uint8_t reg, uint16_t *value);  // 读取16位寄存器


esp_err_t zyp_i2c_master_init_enhanced(i2c_port_t i2c_num, int sda_io_num, int scl_io_num);
void i2c_bus_diagnostic(i2c_port_t i2c_num, int sda_pin, int scl_pin);
void enhanced_i2c_scanner(i2c_port_t i2c_num);


void i2c_scan(i2c_port_t i2c_num);

#endif